package com.hfdg.authority.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.hfdg.prototype.base.utils.TimeUtils;
import com.hfdg.authority.mapper.AuthUserMapper;
import com.hfdg.authority.entity.AuthUser;
import com.hfdg.authority.service.AuthUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * @ClassName AuthUserServiceImpl
 * @Description TODO
 * @Author yuelh
 * @Date 2019/1/19 18:26
 * @Version 1.0
 * @Company 会飞的锅工作室
 **/
@Service
public class AuthUserServiceImpl implements AuthUserService {
    @Autowired
    AuthUserMapper authUserMapper;

    @Override
    public String insert(AuthUser authUser) {
        String uid = UUID.randomUUID().toString();
        authUser.setUid(uid);
        authUser.setDelFlag("0");
        authUser.setCreateTime(new Date());
        authUserMapper.insert(authUser);
        return uid;
    }

    @Override
    public Integer update(AuthUser authUser) {
        authUser.setUpdateTime(new Date());
        return authUserMapper.update(authUser);
    }

    @Override
    public Integer  delete(String uid) {
        return authUserMapper.delete(uid);
    }
    @Override
    public Integer batchDelete(String[] uids) {
            return authUserMapper.batchDelete(uids);
    }

    @Override
    public AuthUser get(String uid) {
        return authUserMapper.get(uid);
    }

    @Override
    public List<AuthUser> list(AuthUser authUser) {
    return  authUserMapper.list(authUser);
    }

    @Override
    public PageInfo<AuthUser> getPagingData(int offset, int limit,String orderBy, AuthUser authUser) {
        if(null==orderBy|| StringUtils.isBlank(orderBy)){
        PageHelper.startPage((offset/limit)+1,limit);
        }else{
        PageHelper.startPage((offset/limit)+1,limit,orderBy);
        }
        PageInfo<AuthUser> pageInfo =new PageInfo<>(authUserMapper.list(authUser));
            return pageInfo;
            }
    @Override
    public String insertOrUpdate(AuthUser authUser){
        if(null!=authUser.getUid()){
            this.update(authUser);
        }else{
            this.insert(authUser);
        }
        return authUser.getUid();
    }
    @Override
    public List<Map<String,Object>> listUserTree(){
        return authUserMapper.listUserTree();
    }

    @Override
    public Map<String,Object> getAuthUserByLoginName(String loginName) {
        Map<String,Object> userMap = authUserMapper.getAuthUserByLoginName(loginName);
        return userMap;
    }
}
